package cn.jinjimi.app.main.extend;

import android.view.View;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.widget.ToolBar.ToolbarHelper;
import cn.jinjimi.app.main.widget.ToolBar.WebToolbar;
import cn.jinjimi.app.util.PreventContinuousClickUtil;

/**
 * Created by Roy
 * Date: 16/7/25
 */
public abstract class ToolBarWebActivity extends BaseActivity implements View.OnClickListener {

    private ToolbarHelper mToolbarHelper;
    private WebToolbar mToolbar;

    @Override
    public void setContentView(int layoutResId) {
        mToolbarHelper = new ToolbarHelper(this, layoutResId);
        mToolbar = mToolbarHelper.createWebToolbar();
        mToolbar.setOnClickListener(this);
        setContentView(mToolbarHelper.getContentVew());
        setSupportActionBar(mToolbar);
    }

    @Override
    public View getContentView() {
        return mToolbarHelper != null ? mToolbarHelper.getContentVew() : null;
    }

    public WebToolbar getTitleBar() {
        return this.mToolbar;
    }

    public ToolbarHelper getToolbarHelper() {
        return mToolbarHelper;
    }

    /**
     * 仅设置标题文字
     *
     * @param titleId
     */
    public void setOnlyTitle(int titleId) {
        if (mToolbar != null)
            mToolbar.setTitle(titleId);
    }

    /**
     * 设置标题文字
     * (non-Javadoc)
     *
     * @see android.app.Activity#setTitle(CharSequence)
     */
    public void setTitle(CharSequence title) {
        super.setTitle(title);
        if (mToolbar != null)
            mToolbar.baseTitle(title);
    }


    /**
     * 设置标题文字
     * (non-Javadoc)
     *
     * @see android.app.Activity#setTitle(int)
     */
    public void setTitle(int titleId) {
        super.setTitle(titleId);
        if (mToolbar != null)
            mToolbar.baseTitle(getString(titleId));
    }



    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
            getSupportFragmentManager().popBackStack();
        } else {
            if (!isTaskRoot())
                super.onBackPressed();
        }
    }

    @Override
    public void onClick(View v) {
        if (PreventContinuousClickUtil.isContinuousClick())
            return;
        switch (v.getId()) {
            case R.id.toolbar_back_btn:
            case R.id.toolbar_back_tx:
                onBackPressed();
                break;

            case R.id.toolbar_close_btn:
            case R.id.toolbar_close_tx:
                finish();
                break;
        }
    }

}

